package com.qingke.servlet;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Cookies2
 */
@WebServlet("/Cookies2")
public class Cookies2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Cookies2() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cookie[] mycookies=request.getCookies();
		if(mycookies==null){
			mycookies=new Cookie[0];
		}
		for(Cookie ck:mycookies){
			System.out.println(ck.getName()+":"+ck.getValue());
		}
		System.out.println("==================================");
		response.getWriter().append("Served at: ").append(request.getContextPath());
		String sessionId=request.getSession().getId();
		Calendar cal=Calendar.getInstance();
		Cookie cookie1=new Cookie("mysessionId",sessionId);
		cookie1.setMaxAge(10*60);
		Cookie cookie2=new Cookie("myTime",String.valueOf(cal.getTimeInMillis()));
		response.addCookie(cookie1);
		response.addCookie(cookie2);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
